package com.xoa.global.component;

import com.xoa.dao.common.SysParaMapper;
import com.xoa.service.WFE.WFERunner.WFEFlowRunPrcs;
import com.xoa.service.attend.AttendService;
import com.xoa.service.document.DocumentSerivce;
import com.xoa.service.financialManage.contract.ContractManageService;
import com.xoa.util.common.StringUtils;
import com.xoa.util.scheduler.DefaultSchedulingConfigurer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.ParseException;

public class SchedulerTask implements InitializingBean {

    @Resource
    private DefaultSchedulingConfigurer defaultSchedulingConfigurer;

    @Resource
    ContractManageService contractManageService;

    @Resource
    WFEFlowRunPrcs wfeFlowRunPrcs;

    @Resource
    AttendService attendService;
    @Resource
    DocumentSerivce documentSerivce;
    @Resource
    private SysParaMapper sysParaMapper;
    @Override
    public void afterPropertiesSet(){
        new Thread() {
            public void run()
            {

                try
                {
                    // 等待任务调度初始化完成
                    while (!defaultSchedulingConfigurer.inited())
                    {
                        Thread.sleep(2000);
                    }
                }
                catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                timingTask();
            }
        }.start();
    }


    /**
     * @接口说明: 定时任务
     * @日期: 2019/12/12
     * @作者: 张航宁
     */
    public void timingTask(){

        defaultSchedulingConfigurer.addTriggerTask("timing_task", new TriggerTask(new Runnable() {
            @Override
            public void run()
            {
                // 定时任务执行方法区域
                //contractManageService.expirationReminder();//合同管理 合同提醒
            }
        }, new CronTrigger("0 0 0/1 * * ?"))); // 每一小时执行一次


        defaultSchedulingConfigurer.addTriggerTask("timing_task2", new TriggerTask(new Runnable() {
            @Override
            public void run()
            {
                // 定时任务执行方法区域
               //wfeFlowRunPrcs.selectTimeOuts();//超时自动办结暂停
            }
        }, new CronTrigger("* 0/10 * * * ?"))); // 每十分钟执行一次

        defaultSchedulingConfigurer.addTriggerTask("timing_task3", new TriggerTask(new Runnable() {
            @Override
            public void run()
            {
                // 定时任务执行方法区域

                try {
                    attendService.addAttendances();
                } catch (ParseException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

            }
        }, new CronTrigger("0 0 23 * * ?"))); // 同步考勤机每天晚上23时执行

        defaultSchedulingConfigurer.addTriggerTask("timing_task4", new TriggerTask(new Runnable() {
            @Override
            public void run()
            {
                // 定时任务执行方法区域
                try {

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new CronTrigger("0 0 9 * * ?"))); // 每天早上9时执行  任务和目标将要到期的提醒
        defaultSchedulingConfigurer.addTriggerTask("timing_task5", new TriggerTask(new Runnable() {
            @Override
            public void run()
            {
                // 定时任务执行方法区域
                try {
                    String sysPara = sysParaMapper.querySysPara("MYPROJECT").getParaValue();
                    if(!StringUtils.checkNull(sysPara) && "powerchina".equals(sysPara)){
                        documentSerivce.sendSms();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, new CronTrigger("0 0 9 * * ?")));// 每天早上9时执行 督办流程将到期提醒，固定的流程，只针对cs执行
    }

}
